<?php
namespace app\admin\controller\Parents;
use think\exception\ValidateException;
use app\admin\model\Parents\Children as ChildrenModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Children extends Admin{



	/**
	 *@description 数据列表
	 *@buildcode(true)
	*/
	function index(){
		if (!$this->request->isPost()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$page = $this->request->post('page', 1, 'intval');
			$param = $this->request->post();

			$field = 'children_id,children_name,children_nickname,children_sex,children_birth,children_grade,children_remark,tutoring_subject,parent_name,create_time,update_time';
			$query = ChildrenModel::field($field);

			if(isset($param['children_id']) && !empty($param['children_id'])) {
				$query = $query->where("children.children_id", $param['children_id']);
			}
			if(isset($param['children_name']) && $param['children_name'] != null) {
				$query = $query->where("children.children_name", $param['children_name']);
			}
			if(isset($param['children_sex']) && $param['children_sex'] != null) {
				$query = $query->where("children.children_sex", $param['children_sex']);
			}
			if(isset($param['children_grade']) && $param['children_grade'] != null) {
				$query = $query->where("children.children_grade", $param['children_grade']);
			}
			if(isset($param['tutoring_subject']) && $param['tutoring_subject'] != null) {
				$query = $query->whereFindInSet("children.tutoring_subject", $param['tutoring_subject']);
			}
			$withJoin = [
				'parent'=>explode(',','parent_name'),
			];
			$query = $query->withJoin($withJoin,'left');

			$orderby = ($param['sort'] && $param['order']) ? $param['sort'].' '.$param['order'] : 'children_id desc';

			$res =$query->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

			foreach($res['data'] as $k=>$v){
				if($v['children_grade']){
					$res['data'][$k]['children_grade'] = Db::query("select grade_name from cd_base_grade where grade_id=".$v['children_grade']."")[0]['grade_name'];
				}
				if($v['tutoring_subject']){
					$res['data'][$k]['tutoring_subject'] = Db::query("select GROUP_CONCAT(subject_name) as subject_name from cd_base_subject where subject_id in (".$v['tutoring_subject'].")")[0]['subject_name'];
				}
			}

			$data['status'] = 200;
			$data['data'] = $res;
			$page == 1 && $data['sql_field_data'] = $this->getSqlField('children_grade,tutoring_subject');
			return json($data);
		}
	}


	/**
	 *@description 修改排序开关
	 *@buildcode(true)
	*/
	function updateExt(){
		$postField = 'children_id,';
		$data = $this->request->only(explode(',',$postField),'post');

		if(!$data['children_id']){
			throw new ValidateException ('参数错误');
		}
		ChildrenModel::update($data);

		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/**
	 *@description 添加
	 *@buildcode(true)
	*/
	public function add(){
		$postField = 'children_name,children_nickname,children_sex,children_birth,children_grade,children_remark,tutoring_subject,learning_goal,parent_id,parent_name,create_time,update_time';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\Parents\Children;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['children_birth'] = !empty($data['children_birth']) ? strtotime($data['children_birth']) : '';
		$data['tutoring_subject'] = is_array($data['tutoring_subject']) ? implode(',',$data['tutoring_subject']) : '';
		$data['create_time'] = date('Y-m-d H:i:s');
		$data['update_time'] = date('Y-m-d H:i:s');

		try{
			$res = ChildrenModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/**
	 *@description 修改
	 *@buildcode(true)
	*/
	public function update(){
		$postField = 'children_id,children_name,children_nickname,children_sex,children_birth,children_grade,children_remark,tutoring_subject,learning_goal,parent_id,parent_name,create_time,update_time';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\Parents\Children;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['children_birth'] = !empty($data['children_birth']) ? strtotime($data['children_birth']) : '';
		$data['tutoring_subject'] = is_array($data['tutoring_subject']) ? implode(',',$data['tutoring_subject']) : '';
		$data['update_time'] = date('Y-m-d H:i:s');

		try{
			ChildrenModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/**
	 *@description 修改信息之前查询信息
	 *@buildcode(true)
	*/
	function getUpdateInfo(){
		$id =  $this->request->post('children_id', '', 'serach_in');
		if(!$id){
			throw new ValidateException ('参数错误');
		}
		$field = 'children_id,children_name,children_nickname,children_sex,children_birth,children_grade,children_remark,tutoring_subject,learning_goal,parent_id,parent_name,create_time,update_time';

		$res = ChildrenModel::field($field)->findOrEmpty($id);
		if($res->isEmpty()){
			throw new ValidateException ('信息不存在');
		}
        $res = $res->toArray();
		$res['tutoring_subject'] = !empty($res['tutoring_subject']) ? explode(',',$res['tutoring_subject']) : '';
		return json(['status'=>200,'data'=>$res]);
	}


	/**
	 *@description 删除
	 *@buildcode(true)
	*/
	function delete(){
		$idx =  $this->request->post('children_id', '', 'serach_in');
		if(!$idx){
			throw new ValidateException ('参数错误');
		}
		ChildrenModel::destroy(['children_id'=>explode(',',$idx)],true);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/**
	 *@description 查看详情
	 *@buildcode(true)
	*/
	function detail(){
		$id =  $this->request->post('children_id', '', 'serach_in');
		if(!$id){
			throw new ValidateException ('参数错误');
		}
		$field = 'children_id,children_name,children_nickname,children_sex,children_birth,children_grade,children_remark,tutoring_subject,parent_name,create_time,update_time';
		$res = ChildrenModel::field($field)->findOrEmpty($id);

		if($res->isEmpty()){
			throw new ValidateException ('信息不存在');
		}

		if($res['children_grade']){
			$res['children_grade'] = Db::query("select grade_name from cd_base_grade where grade_id=".$res['children_grade']."")[0]['grade_name'];
		}
		if($res['tutoring_subject']){
			$res['tutoring_subject'] = Db::query("select GROUP_CONCAT(subject_name) as subject_name from cd_base_subject where subject_id in (".$res['tutoring_subject'].")")[0]['subject_name'];
		}
		return json(['status'=>200,'data'=>$res]);
	}


	/**
	 *@description 获取定义sql语句的字段信息
	 *@buildcode(true)
	*/
	function getFieldList(){
		return json(['status'=>200,'data'=>$this->getSqlField('children_grade,tutoring_subject')]);
	}


	/**
	 *@description 获取定义sql语句的字段信息
	 *@buildcode(true)
	*/
	private function getSqlField($list){
		$data = [];
		if(in_array('children_grade',explode(',',$list))){
			$data['children_grades'] = $this->query("select grade_id,grade_name from pre_base_grade",'mysql');
		}
		if(in_array('tutoring_subject',explode(',',$list))){
			$data['tutoring_subjects'] = $this->query("select subject_id,subject_name from pre_base_subject",'mysql');
		}
		return $data;
	}


}